﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.AssginRule
{
    /// <summary>
    /// RightListView 的摘要说明。
    /// </summary>
    public partial class RightListViewPage : Page
    {
        protected EntityType DisplayType
        {
            get;
            private set;
        }

        protected int ObjID
        {
            get;
            private set;
        }

        protected override void OnInit(EventArgs e)
        {
            RightsGrid.SelectedIndexChanged += RightsGrid_SelectedIndexChanged;
            btn_DelRight.ServerClick += btn_DelRight_ServerClick;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            int value;
            if (!int.TryParse(Request.QueryString["ObjID"], out value))
            {
                value = 0;
            }
            ObjID = value;

            if (!int.TryParse(Request.QueryString["DisplayType"], out value))
            {
                value = (int)EntityType.Position;
            }
            DisplayType = (EntityType)value;

            if (!Page.IsPostBack)
            {
                BindGrid();
            }
            base.OnLoad(e);
        }

        public void BindGrid()
        {
            string spName;
            var db = new Database();

            var parameter = new SqlParameter("@PositionID", SqlDbType.Int);
            parameter.Value = ObjID;
            switch (DisplayType)
            {
                case EntityType.Position:
                    parameter.ParameterName = "@PositionID";
                    spName = "dbo.sproc_GetPositionRightList";
                    break;

                case EntityType.Team:
                    parameter.ParameterName = "@TeamID";
                    spName = "dbo.sproc_GetTeamRightList";
                    break;

                case EntityType.Role:
                    parameter.ParameterName = "@RoleID";
                    spName = "dbo.sproc_GetRoleRightList";
                    break;

                default:
                    throw new InvalidOperationException("Unknow display type: " + DisplayType);
                //错误处理
            }

            DataTable dt;
            using (var dr = db.RunDataReader(spName, new[] { parameter }))
            {
                dt = Tools.ConvertDataReaderToDataTable(dr);
            }

            RightsGrid.DataSource = dt.DefaultView;
            RightsGrid.DataBind();
        }


        private void btn_DelRight_ServerClick(object sender, EventArgs e)
        {
            var list = Tools.FindSelectedItems(RightsGrid, "cb_RightID");
            if (list.Count > 0)
            {
                AssignRights.DeleteRight(list);
                Response.Redirect("RightListView.aspx?ObjID=" + ObjID + "&DisplayType=" + (int)DisplayType);
            }
        }

        private void RightsGrid_SelectedIndexChanged(object sender, EventArgs e)
        { }

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            RightsGrid.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        #endregion
    }
}